<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编号" prop="goodscode">
        <el-input
          v-model="queryParams.goodscode"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsname">
        <el-input
          v-model="queryParams.goodsname"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品规格" prop="goodsspec">
        <el-input
          v-model="queryParams.goodsspec"
          placeholder="请输入商品规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产厂家" prop="manufacturer">
        <el-input
          v-model="queryParams.manufacturer"
          placeholder="请输入生产厂家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品产地" prop="place">
        <el-input
          v-model="queryParams.place"
          placeholder="请输入商品产地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['domain:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['domain:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['domain:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['domain:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsdocList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="商品ID" align="center" prop="goodsid" />-->
      <el-table-column label="商品编号" align="center" prop="goodscode" />
      <el-table-column label="商品名称" align="center" prop="goodsname" />
      <el-table-column label="商品规格" align="center" prop="goodsspec" />
      <el-table-column label="生产厂家" align="center" prop="manufacturer" />
      <el-table-column label="商品产地" align="center" prop="place" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="图片附件" align="center" prop="piclink" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.piclink" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePicture(scope.row)"
            v-hasPermi="['system:pictures:edit']"
          >图片管理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['domain:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['domain:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  <!-- 图片管理弹窗   -->
    <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :model="form2" :rules="rules2" label-width="80px">
        <el-form-item label="分类" prop="noticeType">
          <el-select v-model="queryParamsPic.noticeType" placeholder="请选择图片分类" clearable @change="picFlClick">
            <el-option
              v-for="dict in dict"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="图片附件" prop="piclink">
          <image-upload v-model="form2.picUrl"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改商品资料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业ID" prop="entid">
          <el-input v-model="form.entid" placeholder="请输入企业ID" />
        </el-form-item>
        <el-form-item label="商品编号" prop="goodscode">
          <el-input v-model="form.goodscode" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsname">
          <el-input v-model="form.goodsname" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品规格" prop="goodsspec">
          <el-input v-model="form.goodsspec" placeholder="请输入商品规格" />
        </el-form-item>
        <el-form-item label="生产厂家" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入生产厂家" />
        </el-form-item>
        <el-form-item label="商品产地" prop="place">
          <el-input v-model="form.place" placeholder="请输入商品产地" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="图片附件" prop="piclink">
          <image-upload v-model="form.piclink"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoodsdoc, getGoodsdoc, delGoodsdoc, addGoodsdoc, updateGoodsdoc } from "@/api/flow/goodsdoc";//商品资料
import { listUploadCategory, getUploadCategory, delUploadCategory, addUploadCategory, updateUploadCategory } from "@/api/flow/piclist";//图片管理
import { listCategory, getCategory, delCategory, addCategory, updateCategory } from "@/api/pic/category";//图片分类
import { listMenu, getMenu, delMenu, addMenu, updateMenu } from "@/api/system/menu";//菜单列表-查询菜单列表

export default {
  name: "Goodsdoc",
  data() {
    return {
      // 菜单树选项
      menuOptions: [],
      //图片分类
      queryParamPicFl:{
        pageNum: 1,
        pageSize: 10,
        menuId: 1061,////菜单id,按数据库里代表商品资料界面的id决定,当前为1061,
        menuName: null
      },
      dict:[
        {
          label:'通知',
          value:1
        },
        {
          label:'公告',
          value:2
        }
      ],
      // 是否显示图片管理弹出层
      open2: false,
      // 图片管理表单参数
      form2: {},
      // 图片管理表单校验
      rules2: {
      },
      queryParamsPic: {
        pageNum: 1,
        pageSize: 10,
        moduleId: 1061,//菜单id,按数据库里代表商品资料界面的id决定,当前为1061,
        categoryId: null,//图片分类id,
        picUrl: null,//图片地址
        asId: null,//商品id,实际值为对应选中商品的goodscode
      },

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品资料表格数据
      goodsdocList: [],
      // 弹出层标题
      title: "",
      // 是否显示商品资料弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodscode: null,
        goodsname: null,
        goodsspec: null,
        manufacturer: null,
        place: null,
        unit: null,
        piclink: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listMenu().then(response => {
        this.menuOptions = [];
        const menu = { menuId: 0, menuName: '主类目', children: [] };
        menu.children = this.handleTree(response.data, "menuId");
        this.menuOptions.push(menu);
        console.log('菜单树',this.menuOptions)
      });
    },
    //确定按钮(附带保存图片数据至图片管理)
    submitForm2(){
      //条件判断
      if(this.queryParamsPic.noticeType==null){this.$modal.msgError("请先选择分类");return;}
      if(this.form2.picUrl==null){this.$modal.msgError("请先上传图片");return}
      //执行
      if(this.form2.id==null){//如果没数据,新增图片管理的数据
        console.log('新增form',this.form2)
        this.form2.moduleId=1061//菜单id,按数据库里代表商品资料界面的id决定,当前为1061,
        this.form2.asId=this.queryParamsPic.asId//商品编号
        this.form2.categoryId=this.queryParamsPic.categoryId//商品分类
        // this.form2.picUrl=154//图片路径
        addUploadCategory(this.form2).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open2 = false;
          this.getList();
          this.reset();
        });
      }else {//如果有数据,修改图片管理
        console.log('修改form',this.form2)
        updateUploadCategory(this.form2).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open2 = false;//弹窗隐藏
          this.getList();
          this.reset();
        });
      }
    },
    //图片上传功能弹窗显示
    handlePicture(row){
      this.reset();
      console.log('row',row)
      this.queryParamsPic.asId=row.goodscode//先确定商品id(商品编号)

      listCategory(this.queryParamPicFl).then(response => {
        console.log('图片分类返回数据',response)
        this.dict=[]
        for (let i=0;i<response.rows.length;i++){//录入分类数据
          this.dict.push({label:response.rows[i].picName, value:response.rows[i].picId})
        }
        this.open2 = true;//图片弹窗显示
        this.title = "图片管理";
      });
    },
    //图片分类选择事件+图片查询
    picFlClick(index){
      console.log('图片分类',index,index>0)
      console.log('图片分类2',this.queryParamsPic)
      if(index>0){
        this.queryParamsPic.categoryId=index//绑定选中的商品分类
        //图片查询
        listUploadCategory(this.queryParamsPic).then(response => {
          console.log('form',response)
          console.log('form2',)
          if(response.rows.length>0){//如果有数据
            this.form2 = response.rows[0];
          }else {
            this.form2 = {
              id: null,
              moduleId: null,
              categoryId: null,
              picUrl: null,
              asId: null
            };
          }

        });
      }
    },
    /** 查询商品资料列表 */
    getList() {
      this.loading = true;
      listGoodsdoc(this.queryParams).then(response => {
        this.goodsdocList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsid: null,
        entid: null,
        goodscode: null,
        goodsname: null,
        goodsspec: null,
        manufacturer: null,
        place: null,
        unit: null,
        remark: null,
        piclink: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.form2 = {
        id: null,
        moduleId: null,
        categoryId: null,
        picUrl: null,
        asId: null
      };
      this.$set(this.form,'value','')
      this.queryParamsPic.noticeType=null
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品资料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodsid = row.goodsid || this.ids
      getGoodsdoc(goodsid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品资料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsid != null) {
            updateGoodsdoc(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoodsdoc(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsids = row.goodsid || this.ids;
      this.$modal.confirm('是否确认删除商品资料编号为"' + goodsids + '"的数据项？').then(function() {
        return delGoodsdoc(goodsids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('flow/goodsdoc/export', {
        ...this.queryParams
      }, `goodsdoc_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
